import { defineStore } from "pinia";
import { ref } from "vue";
import { getCurrentUser } from "../apis/user";

export const useLoginUserStore = defineStore("loginUser", () => {
    const loginUser = ref<any>({
        username: "未登录",
    });

    async function fetchLoginUser() {
        const res = await getCurrentUser();
        if (res.data.code === 1 && res.data.data) {
            loginUser.value = res.data.data;
        }else {
            setTimeout(() => {
                loginUser.value = { username: "未登录", id: 1 };
            }, 3000);
        }
    }

    function setLoginUser(newLoginUser: any) {
        loginUser.value = newLoginUser;
    }

    return { loginUser, setLoginUser, fetchLoginUser };
});
